package com.yt.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @Classname HdfsClient
 * @Description
 * @Date 2021/12/3 14:45
 * @Create by 杨涛
 */
public class HdfsClient {
    Configuration configuration;
    FileSystem fs;

    @Before
    public void init() throws IOException, InterruptedException, URISyntaxException {
        // 1 获取文件系统
        configuration = new Configuration();
        // 配置在集群上运行
        // configuration.set("fs.defaultFS", "hdfs://hadoop102:8020");
        // FileSystem fs = FileSystem.get(configuration);
        configuration.set("dfs.replication", "2");
        fs = FileSystem.get(new URI("hdfs://hadoop101:8020"),
                configuration, "yangtao");
    }

    @After
    public void end() throws IOException, InterruptedException, URISyntaxException {
        // 3 关闭资源
        fs.close();
    }

    //创建文件夹
    @Test
    public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
        // 2 创建目录
        fs.mkdirs(new Path("/2101/rengongzhineng/banzhang"));






    }

    // 上传文件
    @Test
    public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
        String [] strs=new String[]{"one","two","three"};
        for(String str:strs){
            fs.copyFromLocalFile(new Path("E:\\local\\"+str+".txt"),new Path("/myfile/"+str+".txt"));
        }
//        fs.copyFromLocalFile(new Path("E:\\local\\one.txt"),new Path("/myfile/one.txt"));
                //new Path("D:\\学校用\\教学\\前内容\\教学课件\\大数据\\学习笔记\\02_hdfs常用操作命令.md")
                // , new Path("/hdfs1.md"));
        System.out.println("over");
    }


    /**
     * 下载文件
     */
    @Test
    public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException {
        // boolean delSrc 指是否将原文件删除
        // Path src 指要下载的文件路径
        // Path dst 指将文件下载到的路径
        // boolean useRawLocalFileSystem 是否开启文件校验
        fs.copyToLocalFile(false, new Path("/hdfs1.md"), new Path("e:/b.md"), true);
    }

}


